﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using JM.JCY.BusinessEntity.PPSM2007;
using JM.JCY.Proxy;
using Microsoft.SharePoint;
using JM.JCY.BusinessEntity;
using System.Linq;
using System.Data;
using JM.JCY.Common;

namespace JM.JCY.SharePointComponent.PersonalInformationDocument.SearchProfile
{
    public partial class SearchProfileUserControl : UserControl
    {
        PPSM2007CodeDataSet.T_PPSM2007CODEDataTable CodeTable;
        int ID = 1;
        string listName = "财产情况";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
              
            }
        }

        public PPSMUserInfoDataSet.UserInfoDataTable GetUserInfoByName(string userName)
        {
            using (PPSM2007Proxy proxy = new PPSM2007Proxy())
            {
                return proxy.GetUserInfoByName(userName);
            }
        }
        public PPSMUserInfoDataSet.UserCareerDataTable GetCareerByName(string userName)
        {
            using (PPSM2007Proxy proxy = new PPSM2007Proxy())
            {
                return proxy.GetCareerByName(userName);
            }
        }

        public PPSMUserEduInfoDataSet.UserEduInfoDataTable GetEduInfoByName(string userName)
        {
            using (PPSM2007Proxy proxy = new PPSM2007Proxy())
            {
                return proxy.GetEduInfoByName(userName);
            }
        }

        public PPSMUserEduInfoDataSet.UserTrainDataTable GetTrainInfoByName(string userName)
        {
            using (PPSM2007Proxy proxy = new PPSM2007Proxy())
            {
                return proxy.GetTrainInfoByName(userName);
            }
        }

        public PPSMUserRelationDataSet.UserRelationDataTable GetRelationInfoByName(string userName)
        {
            using (PPSM2007Proxy proxy = new PPSM2007Proxy())
            {
                return proxy.GetRelationInfoByName(userName);
            }
        }
        public PPSMUserAssessmentDataSet.UserAssessmentDataTable GetAssessmentInfoByName(string userName)
        {
            using (PPSM2007Proxy proxy = new PPSM2007Proxy())
            {
                return proxy.GeAssessmentInfoByName(userName);
            }
        }

        public PPSMUserAssessmentDataSet.UserGloryDataTable GetAwardInfoByName(string userName)
        {
            using (PPSM2007Proxy proxy = new PPSM2007Proxy())
            {
                return proxy.GeGloryInfoByName(userName);
            }
        }
        public PPSMUserAssessmentDataSet.UserPunishInfoDataTable GetPunishInfoByName(string userName)
        {
            using (PPSM2007Proxy proxy = new PPSM2007Proxy())
            {
                return proxy.GetPunishInfoByName(userName);
            }
        }

        public void BindUserInfo(PPSMUserInfoDataSet.UserInfoDataTable userInfoDT)
        {
            var codeTable = CodeTable;
            var row = userInfoDT[0];
            this.lbName.Text = row.UserName;
            this.lbGender.Text = row.UserSex == "1" ? "男" : "女";
            this.lbBirth.Text = string.IsNullOrEmpty(row.UserBirthday) ? string.Empty : row.UserBirthday.Substring(0, 4);

            this.lbNation.Text = string.IsNullOrEmpty(row.UserNationality) ? string.Empty : GetCodeDescByCategoryAndValue(codeTable, "民族", row.UserNationality);
            this.lbBirthplace.Text = string.IsNullOrEmpty(row.UserNativePlace) ? string.Empty : row.UserNativePlace;
            this.lbMarriage.Text = string.IsNullOrEmpty(row.UserMarriageState) ? string.Empty : GetCodeDescByCategoryAndValue(codeTable, "婚姻状况", row.UserMarriageState);
            this.lbHealth.Text = string.IsNullOrEmpty(row.UserHealth) ? string.Empty : GetCodeDescByCategoryAndValue(codeTable, "健康状况", row.UserHealth);
            this.lbPolitics.Text = string.IsNullOrEmpty(row.UserClan) ? string.Empty : GetCodeDescByCategoryAndValue(codeTable, "政治面貌", row.UserClan);
            this.lbEducation.Text = string.IsNullOrEmpty(row.UserNowGraduate) ? string.Empty : GetCodeDescByCategoryAndValue(codeTable, "当前学历", row.UserNowGraduate);
            var userUniversityName = string.IsNullOrEmpty(row.UserUniversityName) ? string.Empty : row.UserUniversityName;
            var userSpecialtyName = string.IsNullOrEmpty(row.UserSpecialtyName) ? string.Empty : row.UserSpecialtyName;
            this.lbSchool.Text = userUniversityName + " (" + userSpecialtyName + ")";
            this.lbPartyPost.Text = string.IsNullOrEmpty(row.UserPartyBusiness) ? string.Empty : GetCodeDescByCategoryAndValue(codeTable, "党组任职职务", row.UserPartyBusiness);
            this.lbAdministrativePost.Text = string.IsNullOrEmpty(row.UserAdminBusiness) ? string.Empty : GetCodeDescByCategoryAndValue(codeTable, "行政职务", row.UserAdminBusiness);
            this.lbLegislationPost.Text = string.IsNullOrEmpty(row.UserLegalBusiness) ? string.Empty : GetCodeDescByCategoryAndValue(codeTable, "法律职务", row.UserLegalBusiness);
            this.lbAdministrativeGrade.Text = string.IsNullOrEmpty(row.UserAdminRank) ? string.Empty : GetCodeDescByCategoryAndValue(codeTable, "行政职级", row.UserAdminRank);
            this.lbJobName.Text = string.IsNullOrEmpty(row.UserWorkerRankName) ? string.Empty : row.UserWorkerRankName;

            if (row.IsImgNull())
                if (row.UserSex == "1")
                    UserImg.ImageUrl = JCYConstant.StaffProfile.DefaultPicturePathMan;
                else
                    UserImg.ImageUrl = JCYConstant.StaffProfile.DefaultPicturePathWoman;
            else
                UserImg.ImageUrl = "~/_Layouts/SharePointComponent/UserImage.aspx?UserName=" + row.UserName;
        }
        protected void dlJob_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            PPSMUserInfoDataSet.UserCareerRow row = (PPSMUserInfoDataSet.UserCareerRow)e.Item.DataItem;
            Label lbID = (Label)e.Item.FindControl("lbID");
            Label lbStartDateTime = (Label)e.Item.FindControl("lbStartDateTime");
            Label lbEndDateTime = (Label)e.Item.FindControl("lbEndDateTime");
            Label lbJobAndPost = (Label)e.Item.FindControl("lbJobAndPost");
            Label lbAdministrativeGrade2 = (Label)e.Item.FindControl("lbAdministrativeGrade2");
            lbID.Text = Convert.ToString(e.Item.ItemIndex + 1);
            lbStartDateTime.Text = string.IsNullOrEmpty(row.BeginDate) ? string.Empty : row.BeginDate.Substring(0, 4).ToString() + "年" + row.BeginDate.Substring(4, 2).ToString() + "月" + row.BeginDate.Substring(6, 2).ToString() + "日";
            lbEndDateTime.Text = string.IsNullOrEmpty(row.EndDate) ? string.Empty : row.EndDate.Substring(0, 4).ToString() + "年" + row.EndDate.Substring(4, 2).ToString() + "月" + row.EndDate.Substring(6, 2).ToString() + "日";
            lbJobAndPost.Text = string.IsNullOrEmpty(row.UserCareer) ? string.Empty : row.UserCareer;
            lbAdministrativeGrade2.Text = string.IsNullOrEmpty(row.AdminRank) ? string.Empty : GetCodeDescByCategoryAndValue(CodeTable, "行政职级", row.AdminRank);
            ID++;
        }

        public PPSM2007CodeDataSet.T_PPSM2007CODEDataTable GetPPSM2007CODEData()
        {
            var dt = new PPSM2007CodeDataSet.T_PPSM2007CODEDataTable();
            using (PPSM2007Proxy proxy = new PPSM2007Proxy())
            {
                return proxy.GetPPSM2007CodeTable();
            }
        }
        public string GetCodeDescByCategoryAndValue(PPSM2007CodeDataSet.T_PPSM2007CODEDataTable dt, string category, string value)
        {
            try
            {
                var codeDesc = from codeTable in dt
                               where codeTable.Category == category && codeTable.CodeValue == value
                               select codeTable.CodeDesc;
                return codeDesc.First();
            }
            catch (Exception)
            {

                return "";
            }
           


        }
        
        protected void ImgBtnSearchProfile_Click(object sender, ImageClickEventArgs e)
        {
            CodeTable = GetPPSM2007CODEData();
            var currentUserName = txtName.Text.Trim();
            DataTable dtPropertyInfo = new DataTable();
            dtPropertyInfo.Columns.Add("UserName");
            dtPropertyInfo.Columns.Add("Income");
            dtPropertyInfo.Columns.Add("House");
            dtPropertyInfo.Columns.Add("Car");
            dtPropertyInfo.Columns.Add("CreateTime");

            SPListItemCollection spListItems = GetPropertyInfoList(currentUserName);
            if (spListItems.Count > 0)
            {
                foreach (SPListItem item in spListItems)
                {
                    DataRow dr = dtPropertyInfo.NewRow();
                    dr["UserName"] = item["干警姓名"].ToString();
                    dr["Income"] = item["收入来源情况"].ToString();
                    dr["House"] = item["房产情况"].ToString();
                    dr["CreateTime"] = item["填表日期"].ToString();
                    dr["Car"] = item["车辆等财产情况"].ToString();
                    dtPropertyInfo.Rows.Add(dr);
                }

                DataRow[] dtPropertyInfoRows = dtPropertyInfo.Select();
                this.dlPropertyInfo.DataSource = dtPropertyInfoRows;
                dlPropertyInfo.DataBind();
                this.lbPropertyNoRecords.Visible = false;

            }
            else
            {
                this.lbPropertyNoRecords.Visible = true;
                dlPropertyInfo.DataSource = null;
                dlPropertyInfo.DataBind();
            }

            
            var userInfo = GetUserInfoByName(currentUserName);
            if (userInfo.Rows.Count > 0)
            {
                BindUserInfo(userInfo);
                this.ProfileSearchResult.Visible = true;
                this.lbNoResultReturn.Visible = false;

            }
            else
            {
                this.lbNoResultReturn.Visible = true;
                this.ProfileSearchResult.Visible = false;
                return;
            }
            //SelfResumeRecord
            DataRow[] rows = GetCareerByName(currentUserName).Select();
            dlJob.DataSource = rows;
            dlJob.DataBind();
            if (rows.Count() > 0)
            {
               
                NoRecord.Visible = false;
            }
               
            else
                NoRecord.Visible = true;
            //EducationAndTrainingRecord

            DataRow[] eduRows = GetEduInfoByName(currentUserName).Select();
            dlEduction.DataSource = eduRows;
            dlEduction.DataBind();
            if (eduRows.Count() > 0)
            {
               
                EducationNoRecord.Visible = false;
            }
               
            else
                EducationNoRecord.Visible = true;


            DataRow[] TrainRows = GetTrainInfoByName(currentUserName).Select();
            dlTrainingRecord.DataSource = TrainRows;
            dlTrainingRecord.DataBind();
            if (TrainRows.Count() > 0)
            {
             
             TrainingNoRecord.Visible = false;
            }
              
            else
                TrainingNoRecord.Visible = true;

           
            //SocialRelationPropertyRecord
            DataRow[] relationRows = GetRelationInfoByName(currentUserName).Select();
            dlSocialRelation.DataSource = relationRows;
            dlSocialRelation.DataBind();
            if (relationRows.Count() > 0)
            {
                
                SocialRelationNoRecord.Visible = false;
            }
               
            else
                SocialRelationNoRecord.Visible = true;

            //YearAssessmentRecord
            DataRow[] assessmentRows = GetAssessmentInfoByName(currentUserName).Select();
            dlYearAssessment.DataSource = assessmentRows;
            dlYearAssessment.DataBind();
            if (assessmentRows.Count() > 0)
            {
               
                YearAssessmentNoRecord.Visible = false;
            }
                
            else
                YearAssessmentNoRecord.Visible = true;

            DataRow[] awardRows = GetAwardInfoByName(currentUserName).Select();
            dlAward.DataSource = awardRows;
            dlAward.DataBind();
            if (awardRows.Count() > 0)
            {
               
                AwardInformationNoRecord.Visible = false;
            }
            else
                AwardInformationNoRecord.Visible = true;

            DataRow[] punishRows = GetPunishInfoByName(currentUserName).Select();
            dlPunish.DataSource = punishRows;
            dlPunish.DataBind();
            if (punishRows.Count() > 0)
            {
                
                PunishInformationNoRecord.Visible = false;
            }
               
            else
                PunishInformationNoRecord.Visible = true;
        }

        protected void dlYearAssessment_ItemDataBound(object sender, DataListItemEventArgs e)
        {
             PPSMUserAssessmentDataSet.UserAssessmentRow row = (PPSMUserAssessmentDataSet.UserAssessmentRow)e.Item.DataItem;
            Label lbYearAssessment = (Label)e.Item.FindControl("lbYearAssessment");
            Label lbAssessmentResult = (Label)e.Item.FindControl("lbAssessmentResult");
            lbYearAssessment.Text = string.IsNullOrEmpty(row.UserTestTime) ? string.Empty : row.UserTestTime;
            lbAssessmentResult.Text = string.IsNullOrEmpty(row.UserTestLevel) ? string.Empty : GetCodeDescByCategoryAndValue(CodeTable, "考核情况",row.UserTestLevel);
        
        }
        

        protected void dlAward_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            PPSMUserAssessmentDataSet.UserGloryRow row = (PPSMUserAssessmentDataSet.UserGloryRow)e.Item.DataItem;
            Label lbAwardDate = (Label)e.Item.FindControl("lbAwardDate");
            Label lbAwardName = (Label)e.Item.FindControl("lbAwardName");
            Label lbAwardPlaceName = (Label)e.Item.FindControl("lbAwardPlaceName");
            lbAwardDate.Text = string.IsNullOrEmpty(row.GloryDate) ? string.Empty : row.GloryDate.Substring(0, 4).ToString() + "年" + row.GloryDate.Substring(4, 2).ToString() + "月" + row.GloryDate.Substring(6, 2).ToString() + "日";
            lbAwardName.Text = string.IsNullOrEmpty(row.GloryArea) ? string.Empty : GetCodeDescByCategoryAndValue(CodeTable, "奖励范围", row.GloryArea);
            lbAwardPlaceName.Text = string.IsNullOrEmpty(row.GloryByUnitName) ? string.Empty : row.GloryByUnitName;

        }

        protected void dlPunish_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            PPSMUserAssessmentDataSet.UserPunishInfoRow row = (PPSMUserAssessmentDataSet.UserPunishInfoRow)e.Item.DataItem;
            Label lbPunishDate = (Label)e.Item.FindControl("lbPunishDate");
            Label lbPunishReason = (Label)e.Item.FindControl("lbPunishReason");
            Label lbPunishNumber = (Label)e.Item.FindControl("lbPunishNumber");
            Label lbPunishPlace = (Label)e.Item.FindControl("lbPunishPlace");
            lbPunishDate.Text = string.IsNullOrEmpty(row.PunishDate) ? string.Empty : row.PunishDate.Substring(0, 4).ToString() + "年" + row.PunishDate.Substring(4, 2).ToString() + "月" + row.PunishDate.Substring(6, 2).ToString() + "日";
            lbPunishReason.Text = string.IsNullOrEmpty(row.PunishReason) ? string.Empty : row.PunishReason;
            lbPunishNumber.Text = string.IsNullOrEmpty(row.PunishFileNumber) ? string.Empty : row.PunishFileNumber;
            lbPunishPlace.Text = string.IsNullOrEmpty(row.PunishByUnitName) ? string.Empty : row.PunishByUnitName;

        }

        protected void dlSocialRelation_ItemDataBound(object sender, DataListItemEventArgs e)
        {
             PPSMUserRelationDataSet.UserRelationRow row = (PPSMUserRelationDataSet.UserRelationRow)e.Item.DataItem;
            Label lbID = (Label)e.Item.FindControl("lbID");
            Label lbName = (Label)e.Item.FindControl("lbName");
            Label lbRelationName = (Label)e.Item.FindControl("lbRelationName");
            Label lbBirth = (Label)e.Item.FindControl("lbBirth");
            Label lbWorkPlace = (Label)e.Item.FindControl("lbWorkPlace");
            Label lbJobTitle = (Label)e.Item.FindControl("lbJobTitle");
            Label lbPoliticalLandscape = (Label)e.Item.FindControl("lbPoliticalLandscape");
            lbID.Text = Convert.ToString(e.Item.ItemIndex + 1);
            ID++;
            lbName.Text = string.IsNullOrEmpty(row.RelationName) ? string.Empty : row.RelationName;
            lbRelationName.Text = string.IsNullOrEmpty(row.UserRelation) ? string.Empty : GetCodeDescByCategoryAndValue(CodeTable, "关系称谓", row.UserRelation);
            lbBirth.Text = string.IsNullOrEmpty(row.Birthday) ? string.Empty : row.Birthday.Substring(0, 4).ToString() + "年";
            lbWorkPlace.Text = string.IsNullOrEmpty(row.WorkDept) ? string.Empty : row.WorkDept;
            lbJobTitle.Text = string.IsNullOrEmpty(row.AdminRank) ? string.Empty : row.AdminRank;
            lbPoliticalLandscape.Text = string.IsNullOrEmpty(row.Clan) ? string.Empty : GetCodeDescByCategoryAndValue(CodeTable, "政治面貌", row.Clan);
        
        }

        protected void dlEduction_ItemDataBound(object sender, DataListItemEventArgs e)
        {

            PPSMUserEduInfoDataSet.UserEduInfoRow row = (PPSMUserEduInfoDataSet.UserEduInfoRow)e.Item.DataItem;
            Label lbEducationType = (Label)e.Item.FindControl("lbEducationType");
            Label lbSchoolName = (Label)e.Item.FindControl("lbSchoolName");
            Label lbProfessionalName = (Label)e.Item.FindControl("lbProfessionalName");
            Label lbNationalEducationType = (Label)e.Item.FindControl("lbNationalEducationType");
            Label lbNationalEducationalBackground = (Label)e.Item.FindControl("lbNationalEducationalBackground");
            Label lbNationalEducationalLevel = (Label)e.Item.FindControl("lbNationalEducationalLevel");
            Label lbPartyEducationalBackground = (Label)e.Item.FindControl("lbPartyEducationalBackground");
            lbEducationType.Text = string.IsNullOrEmpty(row.UserEduArea) ? string.Empty : GetCodeDescByCategoryAndValue(CodeTable, "教育类型", row.UserEduArea);
            lbSchoolName.Text = string.IsNullOrEmpty(row.UserUniversityName) ? string.Empty : row.UserUniversityName;
            lbProfessionalName.Text = string.IsNullOrEmpty(row.UserSpecialtyName) ? string.Empty : row.UserSpecialtyName;
            lbNationalEducationType.Text = string.IsNullOrEmpty(row.EducationSort) ? string.Empty : GetCodeDescByCategoryAndValue(CodeTable, "国民教育类别", row.EducationSort);
            lbNationalEducationalBackground.Text = string.IsNullOrEmpty(row.UserGraduate) ? string.Empty : GetCodeDescByCategoryAndValue(CodeTable, "国民教育学历", row.UserGraduate);
            lbNationalEducationalLevel.Text = string.IsNullOrEmpty(row.UserDegree) ? string.Empty : GetCodeDescByCategoryAndValue(CodeTable, "国民教育学位", row.UserDegree);
            lbPartyEducationalBackground.Text = string.IsNullOrEmpty(row.UserPartyGraduate) ? string.Empty : GetCodeDescByCategoryAndValue(CodeTable, "党校学历", row.UserPartyGraduate);
        }

        protected void dlTrainingRecord_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            PPSMUserEduInfoDataSet.UserTrainRow row = (PPSMUserEduInfoDataSet.UserTrainRow)e.Item.DataItem;
            Label lbStartDateTime = (Label)e.Item.FindControl("lbStartDateTime");
            Label lbEndDateTime = (Label)e.Item.FindControl("lbEndDateTime");
            Label lbTrainingType = (Label)e.Item.FindControl("lbTrainingType");
            Label lbTrainingContent = (Label)e.Item.FindControl("lbTrainingContent");
            Label lbTrainingPlace = (Label)e.Item.FindControl("lbTrainingPlace");
            lbStartDateTime.Text = string.IsNullOrEmpty(row.TrainBeginDate) ? string.Empty : row.TrainBeginDate.Substring(0, 4).ToString() + "年" + row.TrainBeginDate.Substring(4, 2).ToString() + "月" + row.TrainBeginDate.Substring(6, 2).ToString() + "日";
            lbEndDateTime.Text = string.IsNullOrEmpty(row.TrainEndDate) ? string.Empty : row.TrainEndDate.Substring(0, 4).ToString() + "年" + row.TrainEndDate.Substring(4, 2).ToString() + "月" + row.TrainEndDate.Substring(6, 2).ToString() + "日";
            lbTrainingType.Text = string.IsNullOrEmpty(row.TrainSort) ? string.Empty : GetCodeDescByCategoryAndValue(CodeTable, "培训类别", row.TrainSort);
            if (row.TrainSort == "50" || row.TrainSort == "90")
            {
                lbTrainingContent.Text = string.IsNullOrEmpty(row.TrainMatter) ? string.Empty : row.TrainMatter;
            }
            else
            {
                lbTrainingContent.Text = string.IsNullOrEmpty(row.TrainMatter) ? string.Empty : GetCodeDescByCategoryAndValue(CodeTable, "培训内容", row.TrainMatter);
            }
            lbTrainingPlace.Text = string.IsNullOrEmpty(row.TrainLevel) ? string.Empty : GetCodeDescByCategoryAndValue(CodeTable, "培训机构", row.TrainLevel);
        
        }

        private static SPListItemCollection GetPropertyInfoList(string userName)
        {
            string listName = "财产情况";
            SPWeb myWeb = SPContext.Current.Web;
            SPList PropertyInfoList = myWeb.Lists[listName];
            SPQuery query = new SPQuery();
            query.Query = @"<Where>
            <Eq>
            <FieldRef Name='Title'/>
            <Value Type='Text'>" + userName + @"</ValueType>
            </Eq>
            </Where>";
            SPListItemCollection spItem = PropertyInfoList.GetItems(query);
            return spItem;
        }

        protected void dlPropertyInfo_ItemDataBound(object sender, DataListItemEventArgs e)
        {       
                
                DataRow row = (DataRow)e.Item.DataItem;

                Label lbName = (Label)e.Item.FindControl("lbName");
                Label lbCreateTime = (Label)e.Item.FindControl("lbCreateTime");
                Label lbIncome = (Label)e.Item.FindControl("lbIncome");
                Label lbHouse = (Label)e.Item.FindControl("lbHouse");
                Label lbCar = (Label)e.Item.FindControl("lbCar");

                lbName.Text = row["UserName"].ToString();
                lbIncome.Text = row["Income"].ToString();
                lbHouse.Text = row["House"].ToString();
                lbCreateTime.Text = row["CreateTime"].ToString();
                lbCar.Text = row["Car"].ToString();
           
        }
    }
}
